package org.apogee.mil.client.main.pages.admin.forms.widgets;

import java.util.ArrayList;

import org.apogee.mil.client.factories.ServerServiceFactory;
import org.apogee.mil.client.main.pages.admin.forms.AdminTrainerForm;
import org.apogee.mil.client.utils.Session;
import org.apogee.mil.shared.Constants.DisplayFormat;
import org.apogee.mil.shared.Constants.UsersFields;

import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

public class TrainerWidget implements IsWidget {

	private ColumnModel cm;
	private ListStore<BaseModel> store = new ListStore<BaseModel>();
	private Grid<BaseModel> grid;
	private ContentPanel cp; 
	private AdminTrainerForm parent;
	
	public TrainerWidget(AdminTrainerForm parent) {
		this.parent = parent;
	}

	
	@Override
	public Widget asWidget() {
		
		int currentID = Session.getSessionId();
		
		ServerServiceFactory.getAdminService(currentID).getTrainers(new AsyncCallback<ArrayList<BaseModel>>() {
			
			@Override
			public void onSuccess(ArrayList<BaseModel> result) {
				store.removeAll();
				store.add(result);
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox.alert("Error", "Unable to get trainers: " + caught, null);
				
			}
		});
		
		cp = new ContentPanel();
		cp.setHeading("Trainers");
		cp.setHeaderVisible(true);
		cp.setBorders(true);
		cp.setWidth(210);
		
		GridCellRenderer<BaseModel> renderer = new GridCellRenderer<BaseModel>() {

			@Override
			public Object render(BaseModel model, String property, ColumnData config,
					int rowIndex, int colIndex, ListStore<BaseModel> store,
					Grid<BaseModel> grid) {

				String val = (String) (model.get(property) == null ? "No Trainers..." : model.get(property));
				return "<span qtitle='" + cm.getColumnById(property).getHeader() + "' qtip='" + val  
						+ "' style='font-weight: bold;color: #5d8aa8'>" + val + "</span>";
			}
		};

		ArrayList<ColumnConfig> configs = new ArrayList<ColumnConfig>();

		ColumnConfig column = new ColumnConfig();
		column.setId(DisplayFormat.NAME_FMT);
		column.setHeader("Trainer");
		column.setRowHeader(true);
		column.setWidth(205);
		column.setRenderer(renderer);
		configs.add(column);
		
		cm = new ColumnModel(configs);
		
		grid = new Grid<BaseModel>(store, cm);
		grid.setBorders(true);
		grid.setColumnLines(true);
		grid.getAriaSupport().setLabelledBy(cp.getHeader().getId()+"-label");
		grid.setSize(210, 400);
		grid.addListener(Events.CellMouseDown, new Listener<GridEvent<BaseModel>>() {

			@Override
			public void handleEvent(GridEvent<BaseModel> be) {
				if (be == null) {
					return;
				}
				String val = be.getModel().get(UsersFields.USER_ID);
				parent.updateTrainees(Integer.valueOf(val));
				
				
			}
		
		});
		cp.add(grid);
		
		return cp;
	}
	
	public ListStore<BaseModel> getStore() {
		return store;
	}

}
